package com.hzit.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzit.entity.Goods;
import com.hzit.entity.GoodsDesc;
import com.hzit.entity.Item;
import com.hzit.entity.ItemCat;
import com.hzit.entity.group.GoodsGroup;
import com.hzit.mapper.GoodsMapper;
import com.hzit.service.IGoodsDescService;
import com.hzit.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzit.service.IItemCatService;
import com.hzit.service.IItemService;
import net.sf.jsqlparser.util.validation.metadata.DatabaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wf
 * @since 2023-08-07
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {
	@Autowired
	private IGoodsDescService goodsDescService;
	@Autowired
	private IItemCatService itemCatService;
	@Autowired
	private IItemService itemService;

	//1. 保存spu商品、spu商品描述、sku商品列表
	@Override
	@Transactional
	public void save(GoodsGroup goodsGroup) {
		//第一步：保存spu商品信息
		//1.1 得到spu商品对象
		Goods goods = goodsGroup.getGoods();
		goods.setAuditStatus("0");              // 0：代表未审核 1：审核
		//1.2 保存spu商品
		this.save(goods);

		//第二步：上一步保存成功后，得到spu的商品id，并赋值给spu描述表
		//2.1 为spu描述表设置goodsId的值
		GoodsDesc goodsDesc = goodsGroup.getGoodsDesc();
		goodsDesc.setGoodsId(goods.getId());
		//2.2 添加sku描述表
		goodsDescService.save(goodsDesc);

		//第三步：保存sku商品列表
		addItem(goodsGroup);
	}

	//2. 添加sku商品列表
	private void addItem(GoodsGroup goodsGroup) {
		List<Item> items = goodsGroup.getItems();
		Goods goods = goodsGroup.getGoods();
		GoodsDesc goodsDesc = goodsGroup.getGoodsDesc();

		for (Item item : items) {
			//3.1 设置绑定的外键关系
			item.setGoodsId(goods.getId());
			Long brandId = goods.getBrandId();
			//3.2 设置商品分类
			Long category3Id = goods.getCategory3Id();
			ItemCat itemCat = itemCatService.getById(category3Id);
			item.setCategory(itemCat.getName());

			item.setCategoryId(category3Id);
			item.setStatus("1");                    // 状态 1：正常
			item.setTitle(goods.getGoodsName());    // 标题
			item.setCreateTime(LocalDateTime.now());// 创建时间

			// 3.3 设置图片
			String itemImages = goodsDesc.getItemImages();
			if(StrUtil.isNotBlank(itemImages)){
				List<Map> maps = JSON.parseArray(itemImages, Map.class);
				if(!CollectionUtils.isEmpty(maps)){
					Map map = maps.get(0);
					String url = map.get("url") + "";
					item.setImage(url);
				}
			}
			//3.4 设置修改时间
			item.setUpdateTime(LocalDateTime.now());
			//3.5 修改到数据库中
			itemService.save(item);
		}
	}
}
